var lpgl_app = function(){
	var ywy = manager.findParamFromUrl("ywy") || "000000"; 
	var lpglRactive;
	function loadCpgl(){
		lpglRactive = new Ractive({
			el: ".hp-container",
			template: "#lpgl-template",
			oncomplete: function(){
				var titles = [];
				search();
			}
		})
		lpglRactive.on({
			xzlp: function(){
				location.href = "lpgl-xz.html";	
			},
			search: function(){
				var lpmc = $.trim(lpglRactive.get("lpmc"));
				lpglRactive.set("lpmc",lpmc)
				var searchObj = {
						lpmc: lpmc,
						jfks: lpglRactive.get("zsxb"),
						jfjs: lpglRactive.get("zdxb"),
						sjsjks: lpglRactive.get("sjkssj"),
						sjsjjs: lpglRactive.get("sjjssj"),
						lpsyslks: lpglRactive.get("zssy"),
						lpsysljs: lpglRactive.get("zdsy")
				}
				search(searchObj);
			},
			jc: function(e){
				var val = $(e.node).val();
				val = val.replace(/\D/g, "");
				if($(e.node).attr("name") == "dhxbzs"){
					lpglRactive.set("zsxb", val);
				}else if($(e.node).attr("name") == "dhxbzd"){
					lpglRactive.set("zdxb", val);
				}else if($(e.node).attr("name") == "syslzs"){
					lpglRactive.set("zssy", val);
				}else if($(e.node).attr("name") == "syslzx"){
					lpglRactive.set("zdsy", val);
				}
			}
		})
	}
	
	function search(objs) {
        var defaults = {
            pageSize : Constants.page.pagesize,
            currPage : 1,
            ywy: ywy
        }
        // 合并覆盖查询参数
        var objs = $.extend(defaults, objs);// title国际化
        
        var _title = ["礼品名称", "兑换所需积分", "礼品数量", "兑换数", "剩余数量", "礼品状态", "更新时间", "创建日期", "操作"];
        
        initTable(_title, objs);
        
    }
	function initTable(title, param) {
        var data = [{
            name: 'lpmc',
            width: 120,
            float: 'left'
        }, {
            name: 'jf',
            width: 100
        }, {
            name: 'lpsl',
            width: 70
        }, {
            name: 'dhsl',
            width: 70
        }, {
            name: 'sysl',
            width: 70
        }, {
            name: 'zkzt',
            width: 100,
            innerhtml: function(opts){
            	var lpzt;
            	if(opts.zkzt == "01"){
            		lpzt = "未上架";
            	}else{
            		lpzt = "上架";
            	}
                return lpzt;
            }
        }, {
            name: 'operatetime',
            width: 150,
            innerhtml: function(opts) {
                var jbsj = opts.operatetime;
                return moment(jbsj).format("YYYY-MM-DD HH:mm:ss");
            }
        }, {
            name: 'creatertime',
            width: 150,
            innerhtml: function(opts) {
                var jbsj = opts.creatertime;
                return moment(jbsj).format("YYYY-MM-DD HH:mm:ss");
            }
        },  {
            name: 'cz',
            width: 100,
            innerhtml: function (opts) {
                var czStr = "";
                if(opts.zkzt == "01"){
                	czStr += "<a href='javascript:void(0)' class='hp-lpgl-cz' onclick='lpgl_app.lpztcz(\""+opts.id+"\", \"00\")'>上架</a>";
                	czStr += "<a href='lpgl-xg.html?lpid="+opts.id+"' class='hp-lpgl-cz'>修改</a>";
            	}else{
            		czStr += "<a href='javascript:void(0)' class='hp-lpgl-cz orange' onclick='lpgl_app.lpztcz(\""+opts.id+"\", \"01\")'>下架</a>";
                	czStr += "<a href='lpgl-xg.html?lpid="+opts.id+"' class='hp-lpgl-cz'>修改</a>";
            	}
                return czStr;
            }
        }];
        // 绘制列表
        $("#wd-table").wdGrid({
            url: Constants.amUrlPath + "lpgl/getLpxxList",
            colNames: title,
            colModel: data,
            dataType: "GET",
            submitType: "noForm",
            postData: param,
            numberflg: true,
            checkflg: false,
            pager: "#wd-pager",
            rowList: Constants.page.rowList,
            pagesize: Constants.page.pagesize,
    		lineheight: 40
        });
    }
	
	function lpztcz(id, zt){
		var txt = "", stxt = "";
		if(zt == "00"){
			txt = "确认上架该礼品吗？";
			stxt = "上架成功！"
			
		}else{
			txt = "确认下架该礼品吗？";
			stxt = "下架成功！"
		}
		
		var qrParam = {
			txt: txt,
			successCallBackFun: function(){
				var czParam = {
					ids: [id],
					lpzt: zt,
					ywy: ywy
				}
				$.ajax({
					type: "post",
					url: Constants.amUrlPath + "lpgl/updateLpxxZt",
					dataType: "json",
					contentType: "application/json",
					data: JSON.stringify(czParam),
					success: function(data){
						if(data.result){
							lpglRactive.set("lpmc","");
							lpglRactive.set("zsxb","");
							lpglRactive.set("zdxb","");
							lpglRactive.set("sjkssj","");
							lpglRactive.set("sjjssj","");
							lpglRactive.set("zssy","");
							lpglRactive.set("zdsy","");
							search();
							success(stxt);
						}
					}
				})
			}
		}
		confirm(qrParam);
		
		
	}
	
	return {
		init: function(){
			loadCpgl();
		},
		lpztcz: lpztcz
	}
}()

$(document).ready(lpgl_app.init);
